package com.erp.shiro;

import com.erp.entity.Perms;
import com.erp.entity.User;
import com.erp.service.UserService;
import com.erp.util.ApplicationContextUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.List;

/**
 * @author pengyujin
 * 自定义realm
 * @date 2020/11/6 17:05
 */
public class CustomerRealm extends AuthorizingRealm {
    
    @Autowired
    @Lazy
    private UserService userService;
    
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        // 获取身份信息
        String principal = (String) principals.getPrimaryPrincipal();
        System.out.println("===========当前用户是:" + principal);
        
        // 根据主身份信息获取角色和权限信息
        User user = userService.findRolesByUserName(principal);
        if (!CollectionUtils.isEmpty(user.getRoles())) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role->{
                System.out.println(principal + "===============角色" + role.getName());
                simpleAuthorizationInfo.addRole(role.getName());
                
                // 权限信息
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)) {
                    perms.forEach(perm->{
                        if (perm != null) {
                            System.out.println(principal + "===============权限" + perm.getUrl());
                            simpleAuthorizationInfo.addStringPermission(perm.getUrl());
                        }
                    });
                }
            });
            return simpleAuthorizationInfo;
        }
        return null;
    }


    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String principal = (String) token.getPrincipal();

        // 这里没有交由spring进行管理,使用工具类进行获取service
        // 在工厂中获取service对象,bean的名字默认是类名首字母小写
        UserService userService = (UserService) ApplicationContextUtils.getBean("userServiceImpl");
        User user = userService.findByUserName(principal);
        
        // 如果查出来的用户不为空，执行认证
        if (!ObjectUtils.isEmpty(user)) {
            return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());
        }

        return null;
    }
}
